package com.linkivr.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.linkivr.util.GL;

import com.linkivr.model.*;
import com.linkivr.service.*;


@Component("appAction")
public class AppAction extends MyAction {

	private static final long serialVersionUID = 1L;
	private AppService service;
	private UnitService unitService;

	public AppService getService() {
		return service;
	}

	@Resource
	public void setService(AppService service) {
		this.service = service;
	}

	public UnitService getGroupService() {
		return unitService;
	}

	@Resource
	public void setGroupServices(UnitService unitService) {
		this.unitService = unitService;
	}

	public String list() throws IOException {
		GL.info("list()函数  ");
		beginReqParam();

		List<T_app> tabObjs = service.queryAll(start, limit);

		jsonString = "{totalProperty:" + service.queryCount() + ",root:[";
		int i = 0;
		for (T_app to : tabObjs) {

			if (i != 0 && i != tabObjs.size())
				jsonString = jsonString + ",";
			i++;
			String t = "{app_id:'" + to.getApp_id() + "',app_name:'" + to.getApp_name() + "',app_area:'"
					+ to.getApp_area() + "',login_name:'" + to.getLogin_name() + "',unit_id:'"
					+ to.getUnit().getUnit_id() + "',unit_name:'" + to.getUnit().getUnit_name() + "'}";
			jsonString = jsonString + t;
		}
		jsonString += "]}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String listAll() throws Exception {
		GL.info("listAll()函数  ");
		beginReqParam();
		// 获取表达参数
		// ApplicationContext ac = new
		// ClassPathXmlApplicationContext("beans.xml");
		List<T_app> complist = service.queryAll();
		JSONArray array = JSONArray.fromObject(complist);
		out.write(array.toString());
		endReqParam();
		return null;
	}

	public String query() throws IOException {
		GL.info("query()函数  ");

		String jsonString = "";
		beginReqParam();
		// 获取表达参数

		String p_id = request.getParameter("p_id");

		T_app tabObj = service.query(Integer.parseInt(p_id));
		GL.info("query()函数 ok p_id= " + p_id);
		jsonString = "{success: true ,data:{f_app_name:'" + tabObj.getApp_name() + "',f_app_area:'"
				+ tabObj.getApp_area() + "',f_login_name:'" + tabObj.getLogin_name() + "',f_login_passwd:'"
				+ tabObj.getLogin_passwd() + "',f_unit_id:'" + tabObj.getUnit().getUnit_id() + "'}}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String add() throws IOException {
		GL.info("add()函数  ");
		String jsonString = "";
		beginReqParam();
		// 获取表达参数

		String f_app_id = request.getParameter("f_app_id");
		String f_app_name = request.getParameter("f_app_name");
		String f_app_area = request.getParameter("f_app_area");
		String f_login_name = request.getParameter("f_login_name");
		String f_login_passwd = request.getParameter("f_login_passwd");
		String f_unit_id = request.getParameter("f_unit_id");

		T_app to = new T_app();
		to.setApp_id(Integer.parseInt(f_app_id));
		to.setApp_name(f_app_name);
		to.setApp_area(f_app_area);
		to.setLogin_name(f_login_name);
		to.setLogin_passwd(f_login_passwd);

		T_unit unit = unitService.query(Integer.parseInt(f_unit_id));
		to.setUnit(unit);

		service.add(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String delete() throws IOException {
		GL.info("delete()函数  ");
		String jsonString = "";
		beginReqParam();
		// 获取表达参数

		String f_data = request.getParameter("f_data");
		JSONObject a = null;
		Object bean = null;
		JSONArray array = JSONArray.fromObject(f_data);
		Object[] objArray = array.toArray();
		for (Object obj : objArray) {
			a = JSONObject.fromObject(obj);
			bean = JSONObject.toBean(a);
			// 经过查看，发现 bean 为 MorphDynaBean 类型
			MorphDynaBean morphDynaBean = (MorphDynaBean) bean;
			String id = (String) morphDynaBean.get("app_id");
			service.delete(Integer.parseInt(id));

		}
		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String update() throws IOException {
		GL.info("update()函数  ");
		String jsonString = "";
		beginReqParam();
		// 获取表达参数

		String p_id = request.getParameter("p_id");

		String f_app_name = request.getParameter("f_app_name");
		String f_app_area = request.getParameter("f_app_area");
		String f_login_name = request.getParameter("f_login_name");
		String f_login_passwd = request.getParameter("f_login_passwd");
		String f_unit_id = request.getParameter("f_unit_id");

		T_app to = service.query(Integer.parseInt(p_id));
		to.setApp_name(f_app_name);
		to.setApp_area(f_app_area);
		to.setLogin_name(f_login_name);
		to.setLogin_passwd(f_login_passwd);

		T_unit unit = unitService.query(Integer.parseInt(f_unit_id));
		to.setUnit(unit);

		service.update(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

}
